package com.woniuxy.movice.handler;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.movice.dto.OrderInfo;
import com.woniuxy.movice.dto.Round;
import com.woniuxy.movice.entity.Cinema;
import com.woniuxy.movice.entity.Movie;
import com.woniuxy.movice.service.CinemaService;
import com.woniuxy.movice.service.MovieService;
import com.woniuxy.movice.service.OrderService;
import com.woniuxy.movice.service.RoundService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/order")
@Api("订单信息")
public class OrderController {

	@Resource
	private OrderService orderService;
	@Resource
	private CinemaService cinemaService;
	@Resource
	private MovieService movieService;
	@Resource
	private RoundService roundService;
	
	@ApiOperation(value="通过用户id，查询订单信息",notes="存入用户id")
	@RequestMapping(value="/showOrder",method=RequestMethod.POST)
	@ResponseBody
	public List<OrderInfo> showOrder(int uid){
		SimpleDateFormat year = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat month = new SimpleDateFormat("MM-dd HH:mm");
		SimpleDateFormat watchTime = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		List<OrderInfo> orders = orderService.showOrder(uid);
		for (OrderInfo orderInfo : orders) {
			//查询影院
			Cinema cinema = cinemaService.showCinemaById(orderInfo.getYid());
			orderInfo.setCinemaName(cinema.getYname());
			//查询电影
			Movie movie = movieService.findMovieByMid(orderInfo.getMid());
			orderInfo.setMovieName(movie.getMname());
			orderInfo.setMovieImgs(movie.getMovieImgs());
			//查询影厅
			Round round = roundService.findRoundByCid(orderInfo.getCid());
			orderInfo.setHallName(round.getYtname());
			//转换时间
			orderInfo.setoDate(year.format(orderInfo.getOtime()));
			orderInfo.setStartTime(month.format(round.getPstart()));
			orderInfo.setWatchTime(watchTime.format(round.getPstart()));
			//查询订单状态
			String orderState = orderService.findOrderStateByOstate(orderInfo.getOstate());
			orderInfo.setOrderState(orderState);
		}
		return orders;
	}
	
	@ApiOperation(value="通过订单id，软删除",notes="传入订单id")
	@RequestMapping(value="/delOrder",method=RequestMethod.POST)
	@ResponseBody
	public int delOrder(int oid){
		return orderService.delOrderByOid(oid);
	}
	
}
